﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;


namespace SmartSiteManagementSystem.Utils
{
    /// <summary>
    /// 
    /// </summary>

    public class UserUtil
    {
        /// <summary>
        /// Touch使用的登录签名
        /// </summary>
        public static string GenFlagString(string logonName, string logonPwd, int userId, string userIp)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(logonName).Append(logonPwd).Append(userId);
            return EncryptionUtils.MD5(sb.ToString());
        }


        /// <summary>
        /// 对指定字符串加*显示 
        /// </summary>
        /// <param name="type">类型 0用户名 1手机号 2身份证 3Email 以后需要继续加</param>
        /// <param name="hideStr">需要*号显示的字符串</param>
        /// <returns>加*后的字符</returns>
        public static string GetHideForStr(int type, string hideStr)
        {
            string strTemp = "";
            switch (type)
            {
                case 0:
                    strTemp = GetHideUserName(hideStr);
                    break;
                case 1:
                    strTemp = GetHideForMobile(hideStr);
                    break;
                case 2:
                    strTemp = GetHideForIdCard(hideStr);
                    break;
                case 3:
                    strTemp = GetHideForEmail(hideStr);
                    break;
                default:
                    break;
            }
            return strTemp;
        }

        /// <summary>
        /// 获取*号处理过的手机号 
        /// </summary>
        /// <param name="mobileCode"></param>
        /// <returns></returns>
        public static string GetHideForMobile(string mobileCode)
        {
            string result = string.Empty;
            if (!string.IsNullOrEmpty(mobileCode))
            {
                mobileCode = mobileCode.Trim();
                if (mobileCode.Length == 11 && mobileCode.StartsWith("1"))   //手机号码
                {
                    result = string.Format("{0}****{1}", mobileCode.Substring(0, 3), mobileCode.Substring(mobileCode.Length - 4, 4));
                }
                else if (mobileCode.Length == 11 && mobileCode.StartsWith("2"))   //手机号码
                {
                    result = string.Format("{0}***{1}", mobileCode.Substring(0, 3), mobileCode.Substring(mobileCode.Length - 5, 5));
                }
                else if (mobileCode.Length >= 7)
                {
                    result = string.Format("{0}****", mobileCode.Substring(0, mobileCode.Length - 4));
                }
                else
                {
                    result = mobileCode;
                }
            }

            return result;
        }

        /// <summary>
        /// 获取带星号的邮箱 
        /// </summary>
        /// <param name="email">邮箱</param>
        /// <returns>返回格式：起始两位字符 + *号（个数=刨去首尾的剩余长度） + @符号前两位至结束</returns>
        public static string GetHideForEmail(string email)
        {
            if (string.IsNullOrEmpty(email))
            {
                return string.Empty;
            }

            string result = email.Trim();

            if (RegexUtils.IsValidEmail(email))
            {
                int lastLen = email.LastIndexOf('@');
                if (lastLen >= 5)
                {
                    result = string.Format("{0}****{1}", email.Substring(0, 2), email.Substring(lastLen - 2));
                }
                else if (lastLen == 3 || lastLen == 4)
                {
                    result = string.Format("{0}****{1}", email.Substring(0, 1), email.Substring(lastLen - 1));
                }
                else
                {
                    result = email;
                }
            }

            return result;
        }

        /// <summary>
        /// 获取带星号的身份证
        /// </summary>
        /// <param name="idCard"></param>
        /// <returns></returns>
        public static string GetHideForIdCard(string idCard)
        {
            if (string.IsNullOrEmpty(idCard))
            {
                return string.Empty;
            }

            string result = idCard.Trim();

            if (result.Length == 18)
            {
                result = string.Format("{0}********{1}", idCard.Substring(0, 6), idCard.Substring(14));
            }
            else if (result.Length == 15)
            {
                result = string.Format("{0}******{1}", idCard.Substring(0, 6), idCard.Substring(12));
            }

            return result;
        }

        /// <summary>
        /// 获取带星号的用户名 
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static string GetHideUserName(string userName)
        {
            if (string.IsNullOrEmpty(userName))
            {
                return string.Empty;
            }

            string result = userName.Trim();

            if (RegexUtils.IsValidMobile(userName,"86"))
            {
                result = GetHideForMobile(userName);
            }
            else if (RegexUtils.IsValidEmail(userName))
            {
                result = GetHideForEmail(userName);
            }

            return result;
        }



        /// <summary>
        /// 显示用*号处理的用户名，分以下三种情况：
        /// <para>手机号时：前三位****后四位</para>
        /// <para>字母数字（不包含用户名是手机号的）开头的：前三字符*** </para> 
        /// <para>文字开头的：第一个字***最后一个字（只有两个字的，中间也加上***） </para>
        /// </summary>
        /// <param name="sourceName"></param>
        /// <returns></returns>
        public static string GetHideName(string sourceName)
        {
            if (string.IsNullOrEmpty(sourceName))
            {
                return sourceName;
            }

            string result = sourceName;

            if (RegexUtils.IsValidMobile(sourceName,"86"))
            {
                result = string.Format("{0}***", sourceName.Substring(0, 3));
            }
            else
            {
                //当以字母或数字开头时，显示：前三字符***
                if (Regex.IsMatch(sourceName, @"^[a-z0-9]", RegexOptions.IgnoreCase))
                {
                    //注册要求至少4个字符，这里的判断是防止出现意外情况
                    if (sourceName.Length >= 3)
                    {
                        result = string.Format("{0}***", sourceName.Substring(0, 3));
                    }
                }
                else
                {
                    //其它情况（以文字开头的），显示：第一个字***最后一个字（仅两个字的，中间填充***）
                    char[] chs = sourceName.ToCharArray();
                    if (chs.Length > 0)
                    {
                        result = string.Format("{0}***{1}", chs[0], chs[chs.Length - 1]);
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 替换文本中包含的手机号为带*号的 
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string ReplaceMobile(string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return source;
            }

            source = Regex.Replace(source, @"\d{11,}", new MatchEvaluator(MatchMobile));

            return source;
        }

        private static string MatchMobile(Match match)
        {
            if (match.Success && RegexUtils.IsValidMobile(match.Value,"86"))
            {
                return GetHideForMobile(match.Value);
            }

            return match.Value;
        }


    }

}
